﻿namespace UDKDevKitVSXDebugEngine.Extentions
{
    using System;

    public class EqualExpression : Expression
    {
        // temporary class 
        enum operation 
        {
            Eq,
            NotEq
        }

        private static string[] separ = new[] { "=", "!=" };

        private operation oper;
        private string leftExpr;
        private string rightExpr;

        public EqualExpression(string code, Engine engine) : base(code, engine)
        {
            if (code.Contains("!="))
            {
                oper = operation.NotEq;
            }
            else
            {
                oper = operation.Eq;
            }

            var parts = code.Split(separ, StringSplitOptions.RemoveEmptyEntries);
            this.leftExpr = parts.Length > 0 ? parts[0].Trim() : "True";
            this.rightExpr = parts.Length > 1 ? parts[1].Trim() : "True";
        }

        public override object Evaluate()
        {
            var res = new ConstAllowableExpression(this.leftExpr, Engine)
                .Equals(new ConstAllowableExpression(this.rightExpr, Engine));

            return oper == operation.NotEq ? !res : res;
        }

        public override string ToString()
        {
            if (true.Equals(this.Evaluate()))
            {
                return "True";
            }

            return "False";
        }
    }
}
